import { useReactive } from ".";
import { hasOwnProperty, isEqual, isObject } from "../share/utils";

const get = createGetter(),
  set = createSetter();

function createGetter() {
  return function get(target, key, receiver) {
    const res = Reflect.get(target, key, receiver);

    if (isObject(res)) {
      useReactive(res);
    }

    return res;
  };
}

function createSetter() {
  return function set(targe, key, value, receiver) {
    const isKeyExist = hasOwnProperty(targe, key),
      oldValue = targe[key],
      res = Reflect.set(targe, key, value, receiver);
    if (!isKeyExist) {
      console.log("响应式新增");
    } else if (!isEqual(res, oldValue)) {
      console.log("响应式修改");
      // update 视图
    }
    return res;
  };
}

const mutableHandel = {
  get,
  set,
};
export default mutableHandel;
